﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Reflection;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

using mfLogger;

using OST.Shared.Interfaces;
using OST.Persistence;
using OST.Shared;
using OST.Motor;
using OST.Commands;
using OST.Calc.Tracking;


namespace OpenSunTracker
{
    /// <summary>
    /// main/executable entry 4 execution
    /// </summary>
    public class Program
    {
        #region Properties

        /// <summary>
        /// wrapper porperty for mfLogger-singleton
        /// </summary>
        public static mfLogger.mfLogger Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }


        #endregion

        /// <summary>
        /// start-entry
        /// </summary>
        public static void Main()
        {
            try
            {
                InitializeDefaultLogging();

                Logger.Info(Logger.Name + " is booting...");

                //init the object-providers, don't chang order!!!
                InitializeMotorProvider();
                InitializeTrackerProvider();
                InitializeConfigurationProvider();
                InitializeRemoveableMediaStorageProvider();
                InitializeCommands();

                Debug.Print("booted..., " + Debug.GC(true) + " bytes free");
            }
            catch (Exception ex)
            {
                Debug.Print("error in boot-sequence:" + ex.Message);
                Logger.Error("error in boot-sequence:");
                Logger.Error(ex);
            }

            Thread.Sleep(Timeout.Infinite);
        }

        /// <summary>
        /// initializes the implemented REST(rcf)-commands
        /// </summary>
        private static void InitializeCommands()
        {
            CommandInitializer.AppendCommands();
        }

        /// <summary>
        /// initializes the motor-provider
        /// </summary>
        private static void InitializeMotorProvider()
        {
            MotorProvider.Instance.Initialize();
        }

        /// <summary>
        /// initializes the tracker-provider
        /// </summary>
        private static void InitializeTrackerProvider()
        {
            TrackerProvider.Instance.Initialize();
        }

        /// <summary>
        /// initializes the configuration-provider and the re
        /// </summary>
        private static void InitializeConfigurationProvider()
        {
            ConfigurationProvider.Instance.Initialize();

        }

        /// <summary>
        /// initializes the storage-provider
        /// </summary>
        public static void InitializeRemoveableMediaStorageProvider()
        {
            RemoveableMediaStorageProvider.Instance.Initialize();
        }

        private static void InitializeDefaultLogging()
        {
            Logger.Name = Assembly.GetExecutingAssembly().GetName().Name;

#if DEBUG
            Debug.EnableGCMessages(true);
#else
            Logger.Level = enumLogMessageLevel.Info;
            Debug.EnableGCMessages(false);
#endif
        }
    }
}